Skip to content

Some operations on tuples #7057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

anatoliykmetyuk
Copy link
Contributor

Blocked by pickling issues. Also, #7056 prevents using extension methods from the type class.

@anatoliykmetyuk
Copy link
Contributor Author

Pickling issues in question: #7058

@anatoliykmetyuk anatoliykmetyuk self-assigned this Aug 16, 2019
@anatoliykmetyuk anatoliykmetyuk changed the title Zip tuple operation implemented Some operations on tuples Aug 19, 2019
@nicolasstucki
Copy link
Contributor

Here is an implementation of zip without using the Aux anti-pattern. It just needs a match type. Also note that the implementation uses safe casts to avoid the quadratic tuple copy of tail.

object App1 {

  type Zip[T1, T2] <: Tuple = (T1, T2) match {
    case (h1 *: t1, h2 *: t2) => (h1, h2) *: Zip[t1, t2]
    case (Unit, _) => Unit
    case (_, Unit) => Unit
    case _ => Tuple
  }

  def (t1: T1) zip [T1 <: Tuple, T2 <: Tuple](t2: T2): Zip[T1, T2] = {
    if (t1.size == 0 || t2.size == 0) ().asInstanceOf[Zip[T1, T2]]
    else Tuple.fromArray(t1.asInstanceOf[Product].productIterator.zip(t2.asInstanceOf[Product].productIterator).toArray).asInstanceOf[Zip[T1, T2]]
  }

  val x: (1, 2, 3) = (1, 2, 3)
  val y: (4, 5, 6) = (4, 5, 6)

  val z: ((1, 4), (2, 5), (3, 6)) = x.zip(y)

}

@anatoliykmetyuk
Copy link
Contributor Author

Superseded by #7073

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants